#!/bin/sh

ebs_server="https://eulermaker.compass-ci.openeuler.openatom.cn:443"
base_pkg_dir=${HOME}/${package}/${pr_num}
rpm_dir=${base_pkg_dir}/rpms
install_root=${base_pkg_dir}/install-root/${commentid}

CCB=/ccb

function clone_ccb() {
  rm -rf ${CCB}
  git clone https://gitee.com/wanghuan158/ccb.git ${CCB}
  cp -r ${CCB}/sbin/cli ${LKP_SRC}/sbin/
  mkdir ${LKP_SRC}/sbin/lib
  cp -r ${CCB}/lib/opt_parse.rb ${LKP_SRC}/sbin/lib/
}

function write_cli_config(){
    export OAUTH_TOKEN_URL='https://omapi.osinfra.cn/oneid/oidc/token'
    export OAUTH_REDIRECT_URL='https://123.249.10.3:30108/oauth/'
    export PUBLIC_KEY_URL='https://omapi.osinfra.cn/oneid/public/key?community=openeuler'
    
    default="${HOME}/.config/cli/defaults/"
    mkdir -p $default
    cat >${default}/config.yaml <<EOF
GATEWAY_PORT: 443
GATEWAY_IP: eulermaker.compass-ci.openeuler.openatom.cn
SRV_URL: https://eulermaker.compass-ci.openeuler.openatom.cn
SRV_HTTP_REPOSITORIES_PROTOCOL: https://
SRV_HTTP_REPOSITORIES_HOST: eulermaker.compass-ci.openeuler.openatom.cn
SRV_HTTP_REPOSITORIES_PORT: 443
EOF

}

function ccb_download_binarys() {
    echo "============Start download binarys============"
    project=${os_project}
    if [[ "x${package}" == "xkernel" ]]; then
      package="kernel:kernel"
    fi
    download_result=`${CCB}/sbin/cli/ccb download os_project=$project packages=$package architecture=$arch -d -b all`
    if [[ $? -ne 0 || -n $(echo $download_result | grep "Not found") ]]; then
        echo "Failed to download the RPM package generated by $package project $project arch $arch."
        echo "Result: FAILED"
        exit 1
    fi
    binary_folder=$project-$arch-$package
    echo "Download the RPM package generated by $package project $project arch $arch."
    if [[ -d $binary_folder && "$(ls -A $binary_folder | grep '\.rpm$')" ]]; then
        for rpm in $(ls -A $binary_folder | grep '\.rpm$'); do
            right_rpm=$(echo ${rpm%-*-*})
            echo $package":"$right_rpm
        done
        cp $binary_folder/*.rpm $rpm_dir
        ls $binary_folder/
        sudo rm -rf ./$binary_folder
    fi
    echo "<========================================================>"
    ls -l $rpm_dir
    echo "<========================================================>"
    echo "The RPM package generated by the $project is successfully downloaded."
}

function update_repo() {
    echo "=======================Start updating the repo source files====================="
    python_path=${LKP_SRC}/programs/rpm_install_ci
    echo $python_path
    export PYTHONPATH=$python_path
    python3 ${python_path}/update_repo.py -d ${base_pkg_dir} -e ${ebs_server} -b ${build_id} -p ${os_project} -a ${arch}
    cat ${base_pkg_dir}/ci-tools.repo
    echo "=============Update repo source successful============="
}


function install_rpms() {
    install_root=${base_pkg_dir}/install-root/${commentid}

    if [[ ! -d "$install_root" ]]; then
        mkdir -p $install_root
        chmod -R 755 $install_root
    fi
    sudo rm -rf $install_root/*
    echo "=======================Install Check====================="
    # 安装归档的rpm
    if [ -d $rpm_dir ]; then
        cd $rpm_dir
        for rpm in $(ls *.rpm); do
            echo "Start local install $rpm."
            right_rpm=$(echo ${rpm%-*-*})
            start=$(date "+%Y%m%d%H%M%S")
            sudo dnf localinstall -y --setopt=reposdir=${base_pkg_dir} --installroot=$install_root $base_pkg_dir/rpms/$rpm 2>&1 | tee -a $base_pkg_dir/$right_rpm.log
            if [ $? -eq 0 ] && [ -n "$(grep -E 'Complete!' $base_pkg_dir/$right_rpm.log)" ]; then
                echo "The $right_rpm is successfully installed on the local."
                echo $right_rpm":"$start":"$(date "+%Y%m%d%H%M%S")":""success" >>$base_pkg_dir/installed
                echo "Result: SUCCESS"
            else
                echo "Failed local installing $right_rpm."
                echo $right_rpm":"$start":"$(date "+%Y%m%d%H%M%S")":""failed" >>$base_pkg_dir/installed
                echo "Result: FAILED"
            fi
        done
    fi
}
set -x
echo  ${os_project}
echo  ${package}
echo  ${arch}
echo  ${pr_num}
echo  ${commentid}
echo  "job_id: ${id}"
echo  "result_root: ${result_root}"
echo  "Result: FAILED"
mkdir -p $rpm_dir $install_root

if [ ${build_id} -eq 0 ]; then
    echo "Failed to obtain the build_id"
    exit 1
fi

clone_ccb
write_cli_config
ccb_download_binarys
update_repo
install_rpms
set +x

